package com.zhenhe.api.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zhenhe.core.common.utils.IdGen;
import com.zhenhe.core.dto.APIResultDto;
import com.zhenhe.core.dto.APISpecDto;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

@Api(value = "/api/demo", description = "演示如何使用API文档") // Swagger annotation
@RestController
@RequestMapping("/api/demo")
public class APITestController {
	private static final Logger logger = LoggerFactory.getLogger(APITestController.class);

	@ApiOperation(value = "数据查询演示-客户端APP版本号")
	@RequestMapping(value = "/appversion", method = RequestMethod.GET)
	public APIResultDto<Map<String, String>> queryAppVersion() {
		logger.info("查询更新新版本号");
		Map<String, String> map = new HashMap<String, String>();
		map.put("机型", "android");
		map.put("version", "1.0");
		
		APIResultDto<Map<String, String>> dto = new APIResultDto<Map<String, String>>();
		dto.setData(map);
		dto.setStatus(1);
		return dto;
	}

	@ApiOperation(value = "客户端会员登录, 第一个参数是会员手机号码，第二个参数是会员密码")
	@RequestMapping(value = "/applogin", method = RequestMethod.GET)
	public APIResultDto<Map<String, String>> login(@RequestParam String cellPhone, @RequestParam String password) {
		logger.info("客户端会员登录, cellphone : " + cellPhone);
		Map<String, String> map = new HashMap<String, String>();
		String token = IdGen.uuid();
		map.put("token", token);
		APIResultDto<Map<String, String>> dto = new APIResultDto<Map<String, String>>();
		dto.setData(map);
		dto.setStatus(1);
		return dto;
	}
	
	@ApiOperation(value = "数据删除方法演示, 参数APISpecDto")
	@RequestMapping(value="delete", method = RequestMethod.DELETE)
	public APIResultDto<String> delete(@RequestBody APISpecDto dto ){
		
		logger.info("delete-demo-api-call...");
		APIResultDto<String> result = new APIResultDto<String>();
		result.setStatus(APIResultDto.SUCCESS_OK);
		result.setData(null);
		return result;
	}
	
	@ApiOperation(value = "数据提交方法演示, 参数APISpecDto, json格式", responseClass = "APISpecDto")
	@RequestMapping(value="create", method = RequestMethod.POST)
	public APIResultDto<APISpecDto> create(@RequestBody APISpecDto dto ){
		
		logger.info("create-demo-api-call...");
		APIResultDto<APISpecDto> result = new APIResultDto<APISpecDto>();
		result.setStatus(APIResultDto.SUCCESS_OK);
		dto = new APISpecDto();
		dto.setApiName("demo-演示");
		dto.setAuthor("zhigang");
		dto.setDataFormat("json");
		dto.setDescription("演示API如何使用");
		dto.setTransport("HTTPS");
		result.setData(dto);
		return result;
	}
	
	@ApiOperation(value = "数据更新方法演示, 参数APISpecDto, json格式")
	@RequestMapping(value="update", method = RequestMethod.PUT)
	public APIResultDto<APISpecDto> update(@RequestBody APISpecDto dto ){
		
		logger.info("update-demo-api-call...");
		APIResultDto<APISpecDto> result = new APIResultDto<APISpecDto>();
		result.setStatus(APIResultDto.SUCCESS_OK);
		dto = new APISpecDto();
		dto.setApiName("demo-演示");
		dto.setAuthor("zhigang");
		dto.setDataFormat("json");
		dto.setDescription("演示API如何使用");
		dto.setTransport("HTTPS");
		result.setData(dto);
		return result;
	}

}
